/*
 * @Description: axios中间件（初始化和全局配置）
 * @Autor: HuiSir<273250950@qq.com>
 * @Date: 2020-08-06 13:16:24
 * @LastEditTime: 2024-05-28 16:21:43
 */
import axios from "../../utils/axios.esm.js";

//初始化
const Axios = axios.create({
  baseURL: window.isDev
    ? "https://code.zuifengyun.com/wp-json"
    : location.origin + "/wp-json", //api路径
  timeout: 10000, //超时
});

//响应后钩子
Axios.interceptors.response.use(
  (response) => {
    // 对响应数据做些事
    if (response.status !== 200) {
      console.error("[Request error]: " + response.statusText);
    }else if (response.headers["content-type"].includes('application/json') && typeof response.data === "string"){
      try {
        response.data = JSON.parse(response.data);
      } catch (error) {
        return new Error("[JSON-Parse error]: " + error);
      }
    }
    return response.data;
  },
  (error) => {
    return new Error("[Request error]: " + error);
  }
);

export default Axios;
